package kd.bos.kdec.pincar;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

/**
 * 拼车订单
 * 
 * @author zhangyizhe
 *
 */
public class PinCarForMobOrder extends AbstractFormPlugin {

	private String[] buttons = new String[] { "commit", "recommit", "return" };

	@Override
	public void registerListener(EventObject e) {
		super.registerListener(e);
		this.addClickListeners(this.buttons);
	}

	@Override
	public void click(EventObject evt) {
		super.click(evt);
		FormShowParameter parameter = this.getView().getFormShowParameter();
		Control control = (Control) evt.getSource();
		String key = control.getKey();
		IFormView view = this.getView();
		IDataModel model = this.getModel();
		if (StringUtils.equals("commit", key)) {// 点击接单
			long startTime = System.currentTimeMillis();    //获取开始时间

			Long billPk = (Long) parameter.getCustomParams().get("pk");
			long userId = Long.parseLong(RequestContext.get().getUserId());
			DynamicObject obj = BusinessDataServiceHelper.loadSingleFromCache(billPk, "kdec_pincarbill");
			obj.set("orderstatus", 1);
			obj.set("driverid", userId);
			SaveServiceHelper.save(new DynamicObject[] { obj });
			
			long endTime = System.currentTimeMillis();    //获取结束时间
			System.out.println("程序运行时间：" + (endTime - startTime) + "ms");    //输出程序运行时间
			
			// 回写行程单是否被接单的字段
			view.setVisible(true, "recommit");
		} else if (StringUtils.equals("recommit", key)) {// 取消订单
			// 回写行程单是否被接单的字段
			Long billPk = Long.valueOf(model.getValue("billpkid").toString());
			DynamicObject obj = BusinessDataServiceHelper.loadSingleFromCache(billPk, "kdec_pincarbill");
			obj.set("orderstatus", 0);
			obj.set("driverid", 0);
			SaveServiceHelper.save(new DynamicObject[] { obj });
			// 更新订单状态加个筛选
			Map<String, Object> formId = new HashMap<>();
			formId.put("formId", "kdec_pincar_driver");
			FormShowParameter showParameter = FormShowParameter.createFormShowParameter(formId);
			showParameter.getOpenStyle().setShowType(ShowType.Floating);
			view.showForm(showParameter);
		} else if (StringUtils.equals("return", key)) {// 返回父单据列表
			String formIdp = parameter.getParentFormId().toString();
			if (StringUtils.equals("kdec_pincarbillmob", formIdp)) {
				formIdp = "kdec_pincar_driver";
			}
			Map<String, Object> formId = new HashMap<>();
			formId.put("formId", formIdp);
			FormShowParameter showParameter = FormShowParameter.createFormShowParameter(formId);
			showParameter.getOpenStyle().setShowType(ShowType.Floating);
			view.showForm(showParameter);
		}
	}

	@Override
	public void beforeClick(BeforeClickEvent evt) {
		super.beforeClick(evt);
		IDataModel model = this.getModel();
		Control control = (Control) evt.getSource();
		String key = control.getKey();
		if (StringUtils.equals("commit", key)) {// 点击接单
			model.setValue("orderstatus", 0);
		} else if (StringUtils.equals("recommit", key)) {// 取消接单
			model.setValue("orderstatus", 9);
		}
	}

	@Override
	public void afterBindData(EventObject e) {
		super.afterBindData(e);
		IDataModel model = this.getModel();
		FormShowParameter parameter = this.getView().getFormShowParameter();
		if (StringUtils.equals(parameter.getParentFormId(), "kdec_pincarordermoblist")
				&& Integer.valueOf(model.getValue("orderstatus").toString()) != 0) {
			this.getView().setVisible(false, "recommit");
		}
		if (parameter.getCustomParams().size() == 0) {
			return;
		} else if (null != parameter.getCustomParams().get("startplace")) {
			Map<String, Object> params = parameter.getCustomParams();
			model.setValue("startplace", params.get("startplace"));
			model.setValue("endplace", params.get("endplace"));
			model.setValue("taxicost", params.get("taxicost"));
			model.setValue("traveldate", params.get("traveldate"));
			model.setValue("memnum", params.get("memnum"));
			model.setValue("distsance", params.get("distsance"));
			model.setValue("timefield", params.get("timefield"));
			model.setValue("phone", params.get("phone"));
			model.setValue("billpkid", params.get("pk"));
		}
	}
}
